const app = getApp()
import { tsFormatTime } from '../../utils/util.js'
import fetch from '../../utils/services'
Page({
  data: {
    list: [],
  },
  onShow: function (options) {
    wx.setNavigationBarTitle({
      title: '语会'
    })
    app.globalData.page = 'index'
    if (wx.getStorageSync('tooken')) {
      this.getList();
    } else {
      app.userInfoReadyCallback = res => {
        if (res != '') {
          this.getList()
        }
      }
    }
  },
  getList: function() {
    wx.showLoading({
      title: '正在加载',
    })
    fetch.post('/userContacts/list', {}).then(data => {
      wx.hideLoading()
      const list = []
      for (let i = 0; i < data.length; i++) {
        for (let j = 0; j < data[i].value.length; j++) {
          list.push({
            avater: data[i].value[j].friendPic,
            name: data[i].value[j].friendName,
            id: data[i].value[j].friendCode,
            type: data[i].value[j].type, //类型 1、一对一关系 2、小组
            contactsCode: data[i].value[j].contactsCode,
          })
        }
      }
      app.globalData.dialogList = list
      this.setData({ list })
    }).catch((err) => {
      wx.hideLoading()
      wx.showToast({
        title: err.message || '获取信息失败',
        icon: 'none',
        duration: 2000
      })
    })
  },
  getTimeStr: (time) => {
    const currentDay = tsFormatTime(new Date(), 'Y-M-D')
    const myDay = tsFormatTime(time, 'Y-M-D')
    if (currentDay === myDay) {
      let hours = Number(tsFormatTime(time, 'h:m').split(':')[0])
      let munites = Number(tsFormatTime(time, 'h:m').split(':')[1])
      let str = ''
      if (hours > 12) {
        str = '下午'
        hours = hours - 12
      } else if (hours === 12) {
        str = '中午'
      } else {
        str = '上午'
      }
      return `${str} ${hours}:${munites}`
    }
    return tsFormatTime(time, 'M-D')
  },
  toMeeting: e => {
    fetch.post('/user/detailsByCode', { userCode: e.currentTarget.dataset.id } ).then(data => {
      wx.hideLoading()
      const userInfo = {
        userId: data.userCode,
        avator: data.userPic,
        status: data.onlineStatus,
        name: data.userName
      }
      app.globalData.userInfo = userInfo
      var contactsCode;
      for (let i = 0; i < app.globalData.dialogList.length; i++) {
        if (e.currentTarget.dataset.id === app.globalData.dialogList[i].id) {
          contactsCode = app.globalData.dialogList[i].contactsCode
          break;
        }
      }
      wx.navigateTo({
      url: '/pages/chat/index?contactsCode=' + contactsCode,
      fail: err => {
        console.log(err)
      }
      })
    }).catch(err => {
      console.log(err)
      wx.hideLoading()
      wx.showToast({
        title: '获取信息失败',
        icon: 'none',
        duration: 2000
      })
    })
    // wx.navigateTo({
    //   url: `/pages/home/index?userCode=${e.currentTarget.dataset.id}`,
    //   fail: err => {
    //     console.log(err)
    //   }
    // })
  },
  toMeeting2: e => {
    wx.navigateTo({
      url: `/pages/chatServices/index`,
      fail: err => {
        console.log(err)
      }
    })
  },
  tosearch: () => {
    wx.navigateTo({
      url: '/pages/search/index'
    })
  },
})
